﻿using PracticeCentre.Business;
using PracticeCentre.Common;
using PracticeCentre.Modeles;
using PracticeCentre.Mvc.Core.Authentication;
using PracticeCentre.Mvc.Helper;
using PracticeCentre.Mvc.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace PracticeCentre.Mvc.Controllers
{
    public class AccountController : BaseController
    {
        private IFormsAuthentication _authentication;
        private JsonResult result;
        // GET: Account
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model)
        {
            if (model.VerificationCode.ToLower() != Session["VerifyCode"].ToString().ToLower())
            {
                result = DataHelper.GetResult(ExecuteState.Failed, "输入验证码不正确!","");                
            }
            else
            {
                //加密用户密码
                model.UserPass = EncryptTool.GetMD5For32(model.UserPass);
                //将页面视图模型与数据库模型进行映射
                SystemUser loginUser = ModelesMapping.Mapping<SystemUser, LoginViewModel>(model);
                //到数据库取出原密码
                SystemUsers systemUsers = new SystemUsers();
                loginUser = systemUsers.GetLoingUser(loginUser);
                //对比登录密码                
                if (loginUser != null)
                {
                    //创建身份票证
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                    1,              // 版本
                    model.UserName, // 用户名
                    DateTime.Now,   // 创建时间
                    DateTime.Now.AddMinutes(60),// 到期时间
                    false,          // 永久
                    JsonTool.JsonSerializer(loginUser) // 用户数据
                    );
                    //加密票据
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    //保存用户登录的票据
                    _authentication = new AspFormsAuthentication();                    
                    _authentication.SetAuthenticationToken(encryptedTicket);
                    //登录信息保存到客户端的cookie中
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                    authCookie.Value = encryptedTicket;
                    authCookie.Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Set(authCookie);
                    //将登陆信息保存到回话中                                        
                    Session["userInfo"] = systemUsers;                    
                    result = DataHelper.GetResult(ExecuteState.Succeed,"登录成功,正在进入系统，请稍等...","");
                }
                else
                {
                    result = DataHelper.GetResult(ExecuteState.Failed,"用户名或密码不正确！","");
                }
            }
            return result;
        }

        public ActionResult SignOut()
        {
            Session["userInfo"] = null;
            _authentication.SignOut();
            return RedirectToAction("Login");
        }
    }
}